<?php

declare(strict_types=1);

namespace App\Model\Auth;



use App\Model\Model;
use Hyperf\Database\Model\Relations\BelongsToMany;
use Hyperf\Database\Model\Relations\HasMany;
use function Hyperf\Config\config;
use function Swoole\Coroutine\Http\request;

/**
 * @property int $id 
 * @property string $username 
 * @property string $account 
 * @property string $password 
 */
class Admin extends Model
{
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'admin';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['id','nickname','account','password','avatar','status'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer'];



    public function setAvatarAttribute($value): void
    {
        //去除域名
        $value = str_replace(config('upload_cdn'), '', $value);
        $this->attributes['avatar'] = $value;
    }
    public function getAvatarAttribute($value): string
    {
        return config('upload_cdn') . $value;
    }


    /**
     * 角色关联
     * @return HasMany
     */
    public function adminRole(): HasMany
    {
        return $this->hasMany(AdminRole::class, 'admin_id', 'id');
    }


    /**
     * 角色关联
     * @return BelongsToMany
     */
    public function role(): BelongsToMany
    {
        return $this->belongsToMany(Role::class, 'admin_role', 'admin_id', 'role_id');
    }
}
